<template>
  <div class="app-container">
    <!-- 账号信息 -->
    <div class="container">
      <h3 class="title">
        <span class="line"></span> 账号信息
      </h3>
      <div class="content">
        <el-row :gutter="20" class="row">
        <el-col :span="6">
          <p class="itemTitle">用户名称: </p>
          <span class="value">{{personInfo.name}}</span>
        </el-col>
        <el-col :span="7">
           <p class="itemTitle">所属单位: </p>
          <span class="value"></span>
        </el-col>
        <el-col :span="7">
           <p class="itemTitle">所属区域: </p>
          <span class="value"></span>
        </el-col>
      </el-row>
       <el-row :gutter="20" class="row">
        <el-col :span="6">
          <p class="itemTitle">所属角色: </p>
          <span class="value"></span>
        </el-col>
        <el-col :span="7">
           <p class="itemTitle">所属部门: </p>
          <span class="value"></span>
        </el-col>
        <el-col :span="7">
           <p class="itemTitle">创建时间: </p>
          <span class="value">{{personInfo.createTime}}</span>
        </el-col>
      </el-row>
      </div>
    </div>
    <!-- 用户信息 -->
    <div class="container">
      <h3 class="title">
       <span class="line"></span>用户信息
      </h3>

      <!-- 修改用户信息按钮 -->
      <!-- <i class="edit-btn el-icon-edit" @click="editUserBtn">修改</i> -->

      <div class="content">
        <el-row :gutter="20" class="row">
        <el-col :span="6">
          <p class="itemTitle">用户姓名: </p>
          <span class="value">{{personInfo.userName}}</span>
        </el-col>
        <el-col :span="7">
           <p class="itemTitle">身份证号: </p>
          <span class="value"></span>
        </el-col>
        <el-col :span="7">
           <p class="itemTitle">联系电话: </p>
          <span class="value">{{personInfo.mobile}}</span>
        </el-col>
      </el-row>
       <el-row :gutter="20" class="row">
        <el-col :span="6">
          <p class="itemTitle">性别: </p>
          <span class="value"></span>
        </el-col>
        <el-col :span="7">
           <p class="itemTitle">职务: </p>
          <span class="value"></span>
        </el-col>
      </el-row>
      </div>
    </div>
    <!-- 账户信息 -->
    <div class="container">
      <h3 class="title">
        <span class="line"></span> 账户信息
      </h3>
      <div class="content">
        <el-row :gutter="20" class="row">
        <el-col :span="6">
           <p class="itemTitle">密码管理: </p>
          <span class="editPassword" @click.stop="editPasswordBtn">修改密码</span>
        </el-col>
      </el-row>
      </div>
    </div>

    <!-- 修改密码窗口 -->
    <el-dialog
      class="edit-password"
      title="修改密码"
      :modal="false"
      :visible.sync="isVisiblePwdDialog"
      :close-on-click-modal="false"
      @close="cancelPwd"
      width="410px">
        <el-form ref="pwdForm" :model="pwdForm" :inline="true" :rules="pwdRules" label-width="90px" autocomplete="off">
          <el-form-item label="原密码:" prop="old">
            <el-input class="ipt" type="password" clearable placeholder="请输入旧密码" v-model.trim="pwdForm.old"></el-input>
          </el-form-item>
          <el-form-item label="新密码:" prop="new">
            <el-input class="ipt" type="password" clearable placeholder="请输入新密码" v-model.trim="pwdForm.new"></el-input>
          </el-form-item>
            <!-- <p class="tip">。</p> -->
          <el-form-item label="确认密码:" prop="againNew">
            <el-input class="ipt" type="password" clearable placeholder="请确认新密码" v-model.trim="pwdForm.againNew" @keyup.enter.native="editPasswordSubmit"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="cancelPwd">取 消</el-button>
          <el-button size="small" type="primary" @click="editPasswordSubmit">确 定</el-button>
        </div>
    </el-dialog>

    <!-- 修改用户信息窗口 -->
    <el-dialog
      class="edit-user"
      title="用户信息修改"
      :modal="false"
      :visible.sync="isVisibleUserDialog"
      :close-on-click-modal="false"
      @close="cancelUser"
      width="420px">
        <el-form :model="userForm" :inline="true" :rules="userRules" label-width="100px" ref="userForm" autocomplete="off">
          <el-form-item label="用户姓名:" prop="name">
            <el-input class="ipt" type="text" clearable placeholder="请输入用户姓名" v-model.trim="userForm.name"></el-input>
          </el-form-item>
          <el-form-item label="身份证号:" prop="idCard">
            <el-input class="ipt" type="text" clearable placeholder="请输入身份证号" v-model.trim="userForm.idCard"></el-input>
          </el-form-item>
          <el-form-item label="联系电话:" prop="tel">
            <el-input class="ipt" type="text" clearable placeholder="请输入联系电话" v-model.trim="userForm.tel"></el-input>
          </el-form-item>
          <el-form-item label="性别:">
            <el-radio-group v-model="userForm.sex">
              <el-radio :label="0">男</el-radio>
              <el-radio :label="1">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="职务:">
            <el-input class="ipt" type="text" clearable placeholder="请输入职务" v-model.trim="userForm.post" @keyup.enter.native="editUserSubmit"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="cancelUser">取 消</el-button>
          <el-button size="small" type="primary" @click="editUserSubmit">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { modifyPwd, adminInfo } from "@/http/apiUrls.js";
import * as token from "@/utils/auth.js";
export default {
  name: 'Profile',
  data() {
    let  checkPasswordEqual = (rule, value, callback) => {
      if(value != this.pwdForm.new){
        return callback(new Error("两次输入密码不一致！"));
      }
      return callback();
    }
    let checkPasswordFormat = (rule, value,callback) => {
      let reg = /^(?=.*[0-9])(?=.*[a-zA-Z]).{8,20}$/;
      if(!reg.test(value)){
        return callback(new Error("密码必须由8~20位的数字、字母（区分大小写）组成"));
      }
      // 不能有空格
      let spaceReg = new RegExp(/\s+/g);
      if(spaceReg.test(value)){
        return callback(new Error("密码中不可包含空格，请重新输入"));
      }
      return callback();
    }
    return {
      personInfo:{},
      isVisiblePwdDialog:false,  //修改密码对话框弹窗显示
      pwdForm:{   // 修改密码信息
        old:"",
        new: "",
        againNew: "",
      },
      pwdRules:{
        old:[
          { required: true, message: '请输入原密码', trigger: 'blur' },       
        ], new:[
          { required: true, message: '请输入新密码', trigger: 'blur' }, 
          {
            validator:checkPasswordFormat,trigger:"blur"
          }   
        ], againNew:[
          { required: true, message: '请确认新密码', trigger: 'blur' }, {
            validator:checkPasswordEqual,trigger:"blur"
          }
        ]
      },
      isVisibleUserDialog: false, //修改信息对话框弹窗显示
      userForm:{ // 用户信息
        name:"",    //用户姓名
        idCard:"",   //身份证号
        tel:"",   //联系电话
        sex:0, //0-男，1-女
        post:"",  //职务
      },
      userRules:{
        name:[
          { required: true, message: '请输入用户姓名', trigger: 'blur' },       
        ], idCard:[
          { required: true, message: '请输入身份证号', trigger: 'blur' },       
        ], tel:[
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ], post:[
          { required: true, message: '请输入职务', trigger: 'blur' }
        ]
      },
    }
  },
  mounted(){
    this.initUserInfo();
  },
  methods:{
    // 用户信息
    initUserInfo(){
      let id = token.getUserInfo().userId;
      console.log(token);
      let url = adminInfo+id;
      this.$getJson(url).then(res => {
        if(res){
          for(let item in res){
            if(res[item]==null || res[item]== ""){
              res[item] = "---";
            }
          }
          this.personInfo = res;

        }
      })
    },
    // 打开修改密码窗口
    editPasswordBtn(){
      this.isVisiblePwdDialog = !this.isVisiblePwdDialog;
    },
    // 打开修改信息窗口
    editUserBtn(){
      this.isVisibleUserDialog = !this.isVisibleUserDialog;
    },
    // 关闭修改密码窗口
    cancelPwd(){
      this.isVisiblePwdDialog = false;
      this.$refs.pwdForm.resetFields();
    },
    // 关闭修改信息窗口
    cancelUser(){
      this.isVisibleUserDialog = false;
      this.$refs.userForm.resetFields();
    },
    // 修改密码按钮
    editPasswordSubmit(){
      this.$refs.pwdForm.validate(valid=>{
        if (!valid) {
          return false;
        }
        this.editPassword();
      })
    },
    // 修改密码方法
    editPassword(){
      const requestData = {
        "confirmPassword": this.pwdForm.againNew,
        "newPassword": this.pwdForm.new,
        "oldPassword": this.pwdForm.old
      };
      this.$postJson(modifyPwd, requestData).then(res=>{
        if(res){
          this.$message({
            type: "success",
            message: "修改密码成功!",
          });
           this.isVisiblePwdDialog = false;
        }
      })
    },
    // 修改信息按钮
    editUserSubmit(){
      this.$refs.pwdForm.validate(valid=>{
        if(!valid){
          return false;
        }
        this.editUser();
      })
    },
    // 修改用户信息方法
    editUser(){

    },
  },
}
</script>

<style lang="scss" scoped>

  .app-container {
    .container {
      border: 1px solid #ccc;
      border-radius: 4px;
      padding:30px 0;
      margin-bottom: 20px;
      position: relative;
      .title {
        color: #656565;
        margin: 0 60px;
        padding:0;
        display:flex;
        font-size: 16px;
        align-items: center;
        .line {
          margin-right: 6px;
          margin-top: 2px;
          display: inline-block;
          width: 6px;
          height: 18px;
          background-color:rgb(68,158,255);
        }
      }
      .edit-btn {
        color: rgb(0,104,206);
        cursor: pointer;
        font-size: 14px;
        position:absolute;
        top: 35px;
        right: 40px;
        }
      .content {
        font-size: 14px;
        padding: 30px 150px 20px;
        .row:nth-child(1){
          margin-bottom: 10px;
        }
        .itemTitle{
          display: inline-block;
          width: 100px;
          text-align: right;
          color: #242624;
          margin: 0;
          margin-right: 10px;
        }
        .value {
          color: #4E5052;
        }
        .editPassword{
          cursor: pointer;
          text-decoration: underline;
          color: rgb(68,158,255);
          user-select: none;
        }
      }
    }
    // 对话框
    ::v-deep .el-dialog {
      border-radius: 6px;
      user-select: none;
      .el-dialog__header {
        padding: 15px 20px;
        background-color:rgb(0,104,206);
        border-top-left-radius: 6px;
        border-top-right-radius: 6px;
        .el-dialog__title { 
          color: #fff;
          font-size: 16px;
          font-weight: bold;
        }
        .el-icon-close:before{
          content: "关闭";
          color: #fff;
          font-size:14px;
        }
      }
      .el-dialog__body {
        padding-bottom: 0;
      }
      .el-dialog__footer {
        border-top: 1px solid rgb(234,234,234);
      }
      .el-button--primary{
        background-color:rgb(0,104,206);
        border-color: rgb(0,104,206);
      }
    }
    // 输入框
    ::v-deep .el-input__inner {
      height: 30px;
      line-height:30px;
    }
    // 修改密码框
    .edit-password ::v-deep .el-dialog {
      position: fixed;
      right: 130px;
      bottom: 8%;
      .tip {
        color: rgb(146,146,146);
        margin: 0 50px 4px 90px;
        font-size: 12px;
      }
     }
    // 修改用户信息框
    .edit-useInfo ::v-deep .el-dialog {
      position: fixed;
    }
  }
</style>